package main;

import java.util.Random;

public class Main {

   public static void main(String[] args) {
      //we will be playing around with this string
      new Main("Tere kool");
   }
   
   private Random rnd;
   private byte[] randoms;
   
   /**
    * Starts up RNG
    * Prints out a test
    */
   public Main(String password) {
      long randomLong = randomNumber(password);
      rnd = new Random(randomLong);
      randoms = new byte[password.length()];
      rnd.nextBytes(randoms);
      
      /*System.out.println(randomNumber("12322222"));
      System.out.println(randomNumber("13222222"));
      if (true) return;*/
      
      //System.out.println(randomNumber(password));
      
      String cryped = encrypt(password);
      String decryped = decrypt(cryped);
      
      System.out.println(cryped);
      System.out.println(decryped);
   }
   
   /**
    * Encrypts the password.
    */
   private String encrypt(String password) {
      char[] chars = password.toCharArray();      
      for (int i = 0; i < chars.length; i++) {
         chars[i] = (char) (chars[i] + randoms[i]);
      }
      return String.valueOf(chars);
   }
   
   /**
    * Decrypts an allready encryped password.
    */
   private String decrypt(String crypted) {
      char[] chars = crypted.toCharArray();
      for (int i = 0; i < chars.length; i++) {
         chars[i] = (char) (chars[i] - randoms[i]);
      }
      return String.valueOf(chars);
   }
   
   /**
    * Finds a random number BASED ON PASSWORD
    */
   private long randomNumber(String password)
   {
      char[] chars = password.toCharArray();
      int total = 0;
      for (char c : chars) {
         total += c * c;
      }
      total *= chars.length;
      Random rnd = new Random(total);
      byte[] randoms = new byte[password.length()];
      rnd.nextBytes(randoms);
      long number = 0;
      for (int i = 0; i < chars.length; i++) {
         byte multiplier = (byte) (randoms[i] & 1);
         if (multiplier == 0) {
            multiplier = -1;
         }
         number += multiplier * chars[i];
      }
      number *= chars.length;
      return number;
   }
}